package com.vuej.util;

import com.vuej.core.ThrowVueFileRenderCallback;
import com.vuej.core.VueFileRenderCallback;
import com.vuej.entity.VueConfiguration;
import com.vuej.entity.VueFile;
import com.vuej.exception.VuejException;

import javax.script.Bindings;

public class VueUtil {


    public static String getMessage(Bindings error) {
        return (String) error.get("message");
    }

    public static void renderToString(VueConfiguration config, String vueContent, VueFileRenderCallback callback) {
        VueFile vueFile = config.getFileParser().parse(vueContent);

        try {
            config.getRender().render(config.getScriptEnvironment(), vueFile, callback);
        } catch (Throwable throwable) {
            if(throwable instanceof VuejException){
                throw (VuejException)throwable;
            }
            throw new VuejException(throwable);
        }
    }

    public static void renderToString(VueConfiguration config, String vueContent, ThrowVueFileRenderCallback callback) {
        renderToString(config, vueContent, (VueFileRenderCallback) callback);
    }


}
